<?php
namespace Home\Controller;
use Think\Controller;
class PostsController extends Controller {
    public function index(){

    	$this->display();
    }


     public function shuju(){
     	$pagenum=isset($_POST['page']) ? intval($_POST['page']) : 1;
	    $rowsnum=isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	    $posts=M("posts");
	    /* 搜索条件 start*/
	    $postList=array();
	    // 获取表所有字段名
	    $Fields=$posts->getDbFields();
	    foreach ($Fields as $key => $value) {
	    	$datas=isset($_POST[$value]) ? mysql_real_escape_string($_POST[$value]) : '';
	    	$map[$value]=array('like',array('%'.$datas.'%'));
	    }

	    /* 搜索条件 end*/
	    $postList=$posts->where($map)->limit(($pagenum-1)*$rowsnum.','.$rowsnum)->order('id asc')->select();
	    $total=$posts->where($map)->count();
	    if ($total==0){
	        $json='{"total":'.$total.',"rows":[]}';
	        echo $json;
	    }else{
	        $json='{"total":'.$total.',"rows":'.json_encode($postList).'}';//重要，easyui的标准数据格式，数据总数和数据内容在同一个json中
	        echo $json;
	    }
    }

    public function out(){

    //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
    import("Org.Util.PHPExcel");
    import("Org.Util.PHPExcel.Writer.Excel5");
    import("Org.Util.PHPExcel.IOFactory.php");
    // 创建一个处理对象实例   
    $objExcel = new \PHPExcel();
    // 创建文件格式写入对象实例, uncomment
    $objWriter = new \PHPExcel_Writer_Excel5($objExcel);
    $objProps = $objExcel->getProperties();       
    $objProps->setCreator("特种车事业部工艺科");
    $objProps->setLastModifiedBy("特种车事业部工艺科");
    $objProps->setTitle("装配问题记录导出");
    $objProps->setSubject("装配问题");
    $objProps->setDescription("装配问题记录导出");
    $objProps->setKeywords("装配问题");
    $objProps->setCategory("装配问题");
    //*************************************
    //设置当前的sheet索引，用于后续的内容操作。
    //一般只有在使用多个sheet的时候才需要显示调用。
    //缺省情况下，PHPExcel会自动创建第一个sheet被设置SheetIndex=0
    $objExcel->setActiveSheetIndex(0);
    $objActSheet = $objExcel->getActiveSheet();
    //设置当前活动sheet的名称
    $date = date("Y-m-d",time());
    $objActSheet->setTitle('装配问题记录导出'.$date);
    
    //*************************************
    //
    //设置宽度，这个值和EXCEL里的不同，不知道是什么单位，略小于EXCEL中的宽度
    $objActSheet->getColumnDimension('A')->setWidth(5);
    $objActSheet->getColumnDimension('B')->setWidth(20);
    $objActSheet->getColumnDimension('C')->setWidth(15);
    $objActSheet->getColumnDimension('D')->setWidth(40);
    $objActSheet->getColumnDimension('E')->setWidth(10);
    $objActSheet->getColumnDimension('F')->setWidth(10);
    $objActSheet->getColumnDimension('G')->setWidth(30);
    $objActSheet->getColumnDimension('H')->setWidth(25);
    $objActSheet->getColumnDimension('I')->setWidth(25);
/*      $objActSheet->getRowDimension('2')->setRowHeight(33); */
    //设置单元格的值
    $objActSheet->setCellValue('A1', '装配问题记录');
    //合并单元格
    $objActSheet->mergeCells('A1:I1');
    $objStyleA1 = $objActSheet->getStyle('A1');
    $objStyleA1->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objFontA1 = $objStyleA1->getFont();
    $objFontA1->setName('宋体');
    $objFontA1->setSize(18);
    $objFontA1->setBold(true); 
    $objActSheet->setCellValue('A2', '序号 ');
    $objActSheet->setCellValue('B2', '车型 ');
    $objActSheet->setCellValue('C2', '创建日期');
    $objActSheet->setCellValue('D2', '问题描述 ');
    $objActSheet->setCellValue('E2', '来源');
    $objActSheet->setCellValue('F2', '责任单位');
    $objActSheet->setCellValue('G2', '原因分析');
    $objActSheet->setCellValue('H2', '问题整改');
    $objActSheet->setCellValue('I2', '评论');
    
     //设置对齐
    $objActSheet->getStyle('G2')->getAlignment()->setWrapText(true);
    $objActSheet->getStyle('H2')->getAlignment()->setWrapText(true);
    $objActSheet->getStyle('A2:I2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
    //加粗
    $objActSheet->getStyle('A2:I2')->getFont()->setBold(true); 
    
    //从数据库取值循环输出
    $posts=M("posts");
    
    $page = isset($_GET['pageNumber']) ? intval($_GET['pageNumber']) : 1;
    $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : 10;
    
    
    
    
    $listdata=array();
    $data=array();
    
    $data=$posts->limit(($page-1)*$pagesize.','.$pagesize)->order('id asc')->select();
    $count=$posts->limit(($page-1)*$pagesize.','.$pagesize)->count();
    for($i=0;$i<$count;$i++){
        $n=$i+3;
        $objActSheet->setCellValue('A'.$n,$i+1);
        $objActSheet->setCellValue('B'.$n, $data[$i][cx]);
        $objActSheet->setCellValue('C'.$n, $data[$i][content]);
        $objActSheet->setCellValue('D'.$n, $data[$i][cx]);
        $objActSheet->setCellValue('E'.$n, $data[$i][xy]);
        $objActSheet->setCellValue('F'.$n, $data[$i][jsh]);
        $objActSheet->setCellValue('G'.$n, $data[$i][lsh]);
        $objActSheet->setCellValue('H'.$n, $data[$i][creatdate]);
        $objActSheet->setCellValue('I'.$n, $data[$i][fkr]);
    }
     $n=$count+3;
    
    $objActSheet->getStyle('C3:C'.$n)->getAlignment()->setWrapText(true);
    $objActSheet->getStyle('D3:D'.$n)->getAlignment()->setWrapText(true);
    $objActSheet->getStyle('E3:E'.$n)->getAlignment()->setWrapText(true);
    $objActSheet->setCellValue('A'.$n, '导出日期：'.date("Y-m-d H:i:s"));
 
    $objActSheet->mergeCells('A'.$n.':I'.$n);
    $objActSheet->getStyle('A'.$n)->getFont()->setBold(true);
    $objActSheet->getStyle('A'.$n)->getFont()-> setSize(14);
    $objActSheet->getStyle('A'.$n.':I'.$n)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
    $objActSheet->getStyle('A'.$n.':I'.$n)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
    $objActSheet->getStyle('A1:I'.$n)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
 

     $styleArray = array(
            'borders' => array(
                    'allborders' => array(
                            //'style' => PHPExcel_Style_Border::BORDER_THICK,//边框是粗的
                            'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
                            //'color' => array('argb' => 'FFFF0000'),
                    ),
            ),
     );
    $objActSheet->getStyle('A1:I'.$n)->applyFromArray($styleArray);//这里就是画出从单元格A5到 
    

 
 $outputFileName =iconv("utf-8", "gb2312","质量信息导出  ".$date.".xls");
    header("Pragma: public");
    header('Expires:0');
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type:application/force-download");
    Header('content-Type:application/vnd.ms-excel;charset=utf-8');
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");
    header("Content-Disposition: attachment;filename=$outputFileName");
    header("Content-Transfer-Encoding:binary");

    $objWriter->save('php://output');
    
    echo $page .$pagesize;
    exit;
 }  


public function add_Posts(){
        $Posts = M("posts");
        if(!empty($_POST)){
            //判断附件是否有上传
            //如果有则实例化Upload，把附件上传到服务器指定位置
            //然后把附件的路径名获得到，存入$_POST
            if(!empty($_FILES)){
    
                $config = array(
                        'rootPath' => './public/',  //根目录
                        'savePath'  => 'upload/', //保存路径
    
                );
    
                //附件被上传到路径：根目录/保存目录路径/创建日期目录
    
                $upload = new \Think\Upload($config);
    
                //uploadOne会返回已经上传的附件信息
                //$z = $upload->uploadOne($_FILES['img']);
                $z =$upload->upload();
    
                if(!$z){
                    $this->error($upload->getError()); //获得上传附件产生的错误信息
    
                }else {
                    //拼装图片的路径名
                    /* for($i=0;$i++;$i<2){
                        $bigimg[$i] = $z[$i]['savepath'].$z[$i]['savename'];
                        $_POST['file_big_img$i'] = $bigimg[$i];
                        //把已经上传好的图片制作缩略图Image.class.php
                        
                    } */
                    $bigimgOK = $z[0]['savepath'].$z[0]['savename'];
                    //$_POST['file_big_img']=$bigimgOK;
                    
                    $bigimgCW = $z[1]['savepath'].$z[1]['savename'];
                    $_POST['file_big_img'] = $bigimgOK.'Fdivide'.$bigimgCW;
                    
                    //把已经上传好的图片制作缩略图Image.class.php
                    $image = new \Think\Image();
    
                
                    //open();打开图像资源，通过路径名找到图像
                    $srcimgOK = $upload->rootPath.$bigimgOK;
                    $image -> open($srcimgOK);
                    $image -> thumb(150,150);  //按照比例缩小
                    $smallimgOK = $z[0]['savepath']."small_".$z[0]['savename'];
                    $image -> save($upload->rootPath.$smallimgOK);
                    $_POST['file_small_img'][0] = $smallimgOK;
                    
                    //open();打开图像资源，通过路径名找到图像
                    $srcimgCW = $upload->rootPath.$bigimgCW;
                    $image -> open($srcimgCW);
                    $image -> thumb(150,150);  //按照比例缩小
                    $smallimgCW = $z[1]['savepath']."small_".$z[1]['savename'];
                    $image -> save($upload->rootPath.$smallimgCW);
                    $_POST['file_small_img'][1] = $smallimgCW;
                }
            }
    
            $Posts -> create(); //收集post表单数据
            $z = $Posts -> add();
            if($z){
                //$this ->success('添加商品成功', U('Goods/showlist'));
                $this->show("质量信息添加成功！！");
    
            } else {
                //$this ->error('添加商品失败', U('Goods/showlist'));
                $this->show("质量信息添加失败！！");
            }
        }else {
        }
    
    }



     public function lists(){
     	
    	$this->display();
    }
     public function listmy(){
     	$this->show("asdasd");
    	// $this->display();
    }
     public function listsFav(){
     	$this->show("asdasd");
    	// $this->display();
    }
      public function listsHf(){
     	$this->show("asdasd");
    	// $this->display();
    }
}